/*
 * Copyright 2008 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.doofer.test

import com.documentum.fc.client.*
import com.documentum.fc.common.*
import com.documentum.com.*
import org.doofer.*

class ContentUtilTest extends DooferBaseTest {

    def params = null
    def su = null
    def cu = null
    final def pathToIniFile = "/Templates/MenuSystemTemplate.ini"

    /**
     * Constructor
     */
    ContentUtilTest() {}

    void setUp() {
        super.setUp()
        params = [username: username, password: password, repository: repository]
        su = new SessionUtil(params)
        cu = new ContentUtil()
    }

    void testGetContent() {
        println "testing getContent()"
        su.useSession { session ->
            def object = session.getObjectByPath(pathToIniFile)
            assert(object)
            def content = cu.getContent(session: session, object: object)
            assert(content)
            StringWriter sw = new StringWriter()
            sw << content.newReader()
            assert(sw)
            sw.close()
            assert(sw =~ /BEGIN MenuItem/) // check string contains a known substring
        }
    }

    void testGetText() {
        println "testing getText()"
        su.useSession { session ->
            def object = session.getObjectByPath(pathToIniFile)
            assert(object)
            def text = cu.getText(session: session, object: object)
            assert(text)
            assert(text =~ /BEGIN MenuItem/) // check string contains a known substring
        }
    }

    void testExportContent() {
        println "testing exportContent()"
        su.useSession { session ->
            def object = session.getObjectByPath(pathToIniFile)
            def target = 'test.txt'
            def file = cu.exportContent(object: object, target: target)
            assert(file)
            assert(file.exists())
            if(file.exists()) file.delete()
        }
    }

    void testSetContent() {
        println "testing setContent()"
        su.useSession { session ->
            def object = session.newObject('dm_document') as IDfDocument
            assert(object)
            object.setContentType('crtext')
            object.link("/$username")
            String text = 'This is a test'
            def content = new ByteArrayOutputStream()
            def sw = new StringWriter()
            content << sw << text
            cu.setContent(object: object, content: content)
            object.save()
            assert(object.content)
            String readText = new DfClientX().ByteArrayInputStreamToString(object.content)
            assert(readText)
            assertEquals(text, readText)
            object.destroy()
        }
    }
}